//
//  LWZFMListViewController.m
//  MusicFM
//
//  Created by 王景欣 on 16/1/13.
//  Copyright © 2016年 LWZ. All rights reserved.
//

#import "LWZFMListViewController.h"

@interface LWZFMListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, copy) NSString *keyUrl;//拼接的URL

@property (nonatomic, copy) NSString *contentTypeUrl;//拼接的URL

@property (nonatomic, retain) NSMutableArray *dataSource;//数据

@property (nonatomic, retain) UIImageView *backImageView;

@property (nonatomic, retain) UITableView *tableView;

@end

@implementation LWZFMListViewController

#pragma mark - 重写初始化方法
- (instancetype)initWithName:(NSString *)navName withKeyURL:(NSString *)keyURL withContentTypeURL:(NSString *)contentTypeURL
{
    self = [super init];
    if (self) {
        
        self.navigationItem.title = navName;
        self.keyUrl = keyURL;
        self.contentTypeUrl = contentTypeURL;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.tintColor = [UIColor redColor];//
    [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    self.view.backgroundColor = [UIColor clearColor];
    self.backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.backImageView.image = [UIImage imageNamed:@"IMG_1487"];
    [self.view addSubview:self.backImageView];
    [self creatTableView];
    [self handleData];
}
#pragma mark - 数据处理
- (void)handleData {
    
//    @"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=iphone&key=%@&pageId=1&pageSize=40"
    
    NSString *str = [NSString stringWithFormat:@"%@", kFMDetailsURL];//不能字符串直等【因为回去双引号里面的东西】
    NSString *string = [NSString stringWithFormat:str, self.contentTypeUrl, self.keyUrl];
    
    [MHNetWorkTask getWithURL:string withParameter:nil withHttpHeader:nil withResponseType:ResponseTypeJSON withSuccess:^(id result) {
//        NSLog(@"🍎%@", result);
        NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:[result valueForKey:@"list"]];
        NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:0];
        
        if ([self.contentTypeUrl isEqualToString:@"anchor"]) {
            
            for (NSDictionary *dic in mutableArray) {
                LWZRankHostModel *hostModel = [[LWZRankHostModel alloc] initWithDataSource:dic];
                [muArr addObject:hostModel];
            }
        }else if ([self.contentTypeUrl isEqualToString:@"album"]) {
            
            for (NSDictionary *dic in mutableArray) {
                LWZRankOtherModel *rankModel = [[LWZRankOtherModel alloc] initWithDataSource:dic];
                [muArr addObject:rankModel];
            }
        }else {
            
            for (NSDictionary *dic in mutableArray) {
                LWZFMRankListModel *hostFirstModel = [[LWZFMRankListModel alloc] initWithDataSource:dic];
                [muArr addObject:hostFirstModel];
            }
        }
        
        self.dataSource = [NSMutableArray arrayWithArray:muArr];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } withFail:^(NSError *error) {
        
    }];
}
#pragma mark - 创建TableView
- (void)creatTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 112) style:UITableViewStyleGrouped];
    self.tableView.separatorColor = [UIColor colorWithRed:0.95 green:0.49 blue:0.61 alpha:1];
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    UINib *nib = [UINib nibWithNibName:@"LWZRankTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"LWZRankTableViewCellID"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LWZRankOtherTableViewCell" bundle:nil] forCellReuseIdentifier:@"LWZRankOtherTableViewCellID"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LWZHostTableViewCell" bundle:nil] forCellReuseIdentifier:@"LWZHostTableViewCellID"];
    [self.view addSubview:self.tableView];
}

#pragma mark - TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if ([self.contentTypeUrl isEqualToString:@"anchor"]) {
        LWZHostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LWZHostTableViewCellID"];
        cell.hostModel = self.dataSource[indexPath.row];
        cell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    }else if ([self.contentTypeUrl isEqualToString:@"album"]) {
        
//        LWZRankOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LWZRankOtherTableViewCellID"];
        LWZRankOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LWZRankOtherTableViewCellID"];
        cell.rankOtherModel = self.dataSource[indexPath.row];
        cell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    }else {
        LWZRankTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LWZRankTableViewCellID"];
        cell.rankModel = self.dataSource[indexPath.row];
        cell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        cell.backgroundColor = [UIColor clearColor];
        return cell;
        
    }
}
#pragma mark - Height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 150;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}
#pragma mark - TableView 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

    if ([self.contentTypeUrl isEqualToString:@"anchor"]) {
        //女主播
        LWZHostTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        LWZExclusiveViewController *exVC = [[LWZExclusiveViewController alloc] initWithStringURL:[NSString stringWithFormat:@"%@", cell.hostModel.uid] WithTitleImageLabelString:cell.hostModel.nickname withImageUrl:cell.hostModel.largeLogo];
        [self.navigationController pushViewController:exVC animated:YES];
    }else if ([self.contentTypeUrl isEqualToString:@"album"]) {
        
        LWZRankOtherTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        LWZFirstOtherRowViewController *firstOtherVc = [[LWZFirstOtherRowViewController alloc] initWithStringURL:cell.rankOtherModel.albumId];
        [self.navigationController pushViewController:firstOtherVc animated:YES];
    }else {
        //第0个section的0row
        LWZFMRankListModel *model = self.dataSource[indexPath.row];
//        LWZFMPlayViewController *playVC = [[LWZFMPlayViewController alloc] init];
        LWZFMPlayViewController *playVC = [LWZFMPlayViewController sharePlayerViewController];
        playVC.song_id = model.trackId;
        playVC.indexRow = indexPath.row;
        [self presentViewController:playVC animated:YES completion:nil];
    }
    

    
    
}

#pragma mark ------------------------------------
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
